package com.example.mybank.springSecurity;

import com.example.mybank.entity.Users;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

public class UserHandler extends Users implements UserDetails {

    Users users;

    List<GrantedAuthority> auths;

    public UserHandler(Users users){
        setUsers(users);
    }

    public Users getUsers(){
        return  users;
    }

    public void setUsers(Users users){
        this.users=users;
        auths= AuthorityUtils.commaSeparatedStringToAuthorityList(
                users.getIs_admin().equals(true)?"admin":"users");  //配置角色
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return auths;
    }

    @Override
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    public String getUsername() {
        return users.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
